package BFS;

import java.util.*;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-09
 * Time:13:46
 * Text：695. 岛屿的最大面积
 */
public class Text3 {int[] dx = {0,0,1,-1};
    int[] dy = {-1,1,0,0};
    int m,n;
    public int bfs(int[][] grid,int j,int k)
    {
        Queue<int[]> q=new LinkedList<>();
        q.add(new int[]{j ,k});
        int count=1;
        grid[j][k] = 0;
        while(!q.isEmpty())
        {
            int[] index=q.poll();
            int x = index[0],y = index[1];
            for(int i = 0;i < 4;i++)
            {
                int x1=dx[i] + x,y1=dy[i] + y;
                if(x1 >= 0 && x1 < n && y1 >=0 && y1 < m && grid[x1][y1] == 1)
                {
                    grid[x1][y1] = 0;
                    q.add(new int[]{x1,y1});
                    count++;
                }
            }
        }
        return count;
    }
    public int maxAreaOfIsland(int[][] grid) {
        n = grid.length;   m = grid[0].length;
        int max=0;
        for(int i = 0; i < n; i++)
        {
            for(int k = 0; k < m; k++)
            {
                if(grid[i][k] == 1)
                {
                    max=Math.max(bfs(grid,i,k) ,max);
                }
            }
        }
        return max;
    }
}